<template>
    <Card>
        <JG-search :searchData="searchData" @on-input-search="askDatasearch"  @on-change-search="askDatasearchs" @on-change-change="askDatas" />
        <JG-table
            :params="params"
            :data="data"
            :loading="loading"
            :total="total"
            :butshow="butshow"
            :columns="columns"
            :maxHeight="700"
            @askData="askData"
            @handleFail="handleFail"
            @sortchange="sortchange"></JG-table>
    </Card>
</template>
<script>
    import { siteindexuploadloglist, siteindexdeleteuploadqueue } from '@api/robotupload'
    export default {
        name: '',
        data () {
            return {
                butshow: {
                    Delete: {
                        name: '-v1-site-index-delete-upload-queue',
                        flag: '/v1/site/index/delete-upload-queue'
                    }
                },
                modal: false,
                params: { domain: '', exec_status: '', page: 1, pageSize: 10, sort_column: '', sort_order: '' },
                loading: false,
                total: 0,
                searchData: {
                    left: [
                        { id: 1, key: 'Input', value: '', clearable: true, placeholder: '请输入站点域名', callback: 'search' },
                        { id: 2, key: 'Select', value: '', style: { width: '200px' }, clearable: true, placeholder: '请选择状态类型', childList: [{ value: '3', label: '成功' }, { value: '4', label: '失败' }], callback: 'change' }
                    ]
                },
                columns: [
                    { title: 'ID', key: 'id', width: 100 },
                    { title: '站点名称', key: 'domain', minWidth: 180, sortable: 'custom' },
                    { title: '最后上传时间', key: 'exec_upload_time_str', minWidth: 170, sortable: 'custom' },
                    { title: '上传类型', key: 'upload_type_str', minWidth: 260, sortable: 'custom' },
                    { title: '任务类型', key: 'type_str', minWidth: 140, sortable: 'custom' },
                    { title: '操作人', key: 'realname', width: 120 },
                    { title: '创建时间', key: 'created_at_str', minWidth: 170, sortable: 'custom' },
                    { title: '状态', key: 'exec_status_str', slot: 'exec_status_str', width: 130, sortable: 'custom' }
                ],
                data: []
            }
        },
        mounted () {
            this.initial()
        },
        methods: {
            initial () {
                this.askData()
            },
            askDatas () {
                this.params.page = 1
                this.askData()
            },
            askDatasearchs () {
                this.searchData.left.forEach((item) => {
                    if (item.id === 1) {
                        item.value = ((item.value.replace(/(^\s*)|(\s*$)/g, '')).replace(/(^\http:\/\/*)|(\/*$)/g, '')).replace(/(^\https:\/\/*)|(\/*$)/g, '')
                    }
                });
                this.askDatasearch()
            },
            askData () {
                this.searchData.left.forEach((item) => {
                    if (item.id === 1) {
                        this.params.domain = item.value
                    } else if (item.id === 2) {
                        this.params.exec_status = item.value
                    }
                });
                this.loading = true
                siteindexuploadloglist(this.params).then(res => {
                    this.total = res.total
                    this.data = res.list
                    this.loading = false
                }).catch(() => {
                    this.loading = false;
                });
            },
            sortchange (e) {
                switch (e.sort) {
                    case 1: this.params.sort_order = 'asc'; break;
                    case 2: this.params.sort_order = 'desc'; break;
                    default: this.params.sort_order = ''; break;
                }
                switch (e.key) {
                    case 'domain': this.params.sort_column = 'domain'; break;
                    case 'created_at_str': this.params.sort_column = 'created_at'; break;
                    case 'exec_upload_time_str': this.params.sort_column = 'exec_upload_time'; break;
                    case 'exec_status_str': this.params.sort_column = 'exec_status'; break;
                    case 'type_str': this.params.sort_column = 'type'; break;
                    case 'upload_type_str': this.params.sort_column = 'upload_type'; break;
                    default: break;
                }
                this.askData()
            },
            askDatasearch () {
                this.params.page = 1
                this.searchData.left.forEach((item) => {
                    if (item.id === 2) {
                        item.value = '';
                    }
                });
                this.askData()
            },
            handleFail (row) {
                this.$Modal.error({
                    title: '失败原因',
                    content: row.reason
                });
            }
        }
    }
</script>
